package com.xyt.imei.core.model.converter;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xyt.imei.core.meta.StoreStatusEnum;
import com.xyt.imei.core.model.po.Store;
import com.xyt.imei.core.model.po.User;
import com.xyt.imei.core.model.vo.StoreVO;
import com.xyt.imei.core.service.UserService;

@Component
public class StoreConverter {
	
	@Autowired
	private UserService userService;

	public List<StoreVO> po2vo(List<Store> storeList) throws Exception {
		
		List<StoreVO> vos = new ArrayList<StoreVO>();
		
		for(Store store : storeList) {
			vos.add(po2vo(store));
		}
		
		return vos;
	}
	
	public List<StoreVO> po2vo(List<Store> storeList, String branchName) throws Exception {
		
		List<StoreVO> vos = new ArrayList<StoreVO>();
		
		for(Store store : storeList) {
			vos.add(po2vo(store, branchName));
		}
		
		return vos;
	}
	
	public List<StoreVO> po2vo(List<Store> storeList, String branchName, boolean hasTargetBranchName) throws Exception {
		
		List<StoreVO> vos = new ArrayList<StoreVO>();
		
		for(Store store : storeList) {
			if(hasTargetBranchName)
				vos.add(po2vo(store, branchName));
			else
				vos.add(po2voExcludeTargetBranchName(store, branchName));
		}
		
		return vos;
	}
	
	public StoreVO po2vo(Store store) throws Exception {
		
		if(store == null)
			return null;
		
		StoreVO vo = new StoreVO();
		BeanUtils.copyProperties(store, vo);
		
		User user = userService.findByPrimaryKey(store.getUserId());
		if(user != null)
			vo.setBranchName(user.getUsername());
		if(Optional.ofNullable(store.getTargetUserId()).orElse(0) != 0) {
			User targetUser = userService.findByPrimaryKey(store.getTargetUserId());
			vo.setTargetBranch(targetUser.getUsername());
		}
			
		vo.setStatus(StoreStatusEnum.valueOf(store.getStatus()));
		
		return vo;
		
	}
	
	public StoreVO po2vo(Store store, String branchName) throws Exception {
		
		StoreVO vo = new StoreVO();
		BeanUtils.copyProperties(store, vo);
		
		vo.setBranchName(branchName);
		vo.setStatus(StoreStatusEnum.valueOf(store.getStatus()));
		
		if(Optional.ofNullable(store.getTargetUserId()).orElse(0) != 0) {
			User targetUser = userService.findByPrimaryKey(store.getTargetUserId());
			vo.setTargetBranch(targetUser.getUsername());
		}
		
		return vo;
		
	}
	
	public StoreVO po2voExcludeTargetBranchName(Store store, String branchName) throws Exception {
		
		StoreVO vo = new StoreVO();
		BeanUtils.copyProperties(store, vo);
		
		vo.setBranchName(branchName);
		vo.setStatus(StoreStatusEnum.valueOf(store.getStatus()));
		
		return vo;
		
	}
	
	public StoreVO po2vo(Store store, String branchName, String targetBranchName) throws Exception {
		
		StoreVO vo = new StoreVO();
		BeanUtils.copyProperties(store, vo);
		
		vo.setBranchName(branchName);
		vo.setTargetBranch(targetBranchName);
		vo.setStatus(StoreStatusEnum.valueOf(store.getStatus()));
		
		return vo;
		
	}
	
}
